package com.wave.wavesystem.ai.config;

import com.wave.wavesystem.ai.advisor.WaveAILogAdvisor;
import com.wave.wavesystem.ai.advisor.WaveTokenCalculateAdvisor;
import com.wave.wavesystem.ai.advisor.factory.PostDocumentRetrieverAdvisorFactory;
import com.wave.wavesystem.ai.chatmemory.WaveChatMemory;
import com.wave.wavesystem.ai.tool.WaveToolCallBackFactory;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@RequiredArgsConstructor
public class ChatClientConfig {

    @Resource
    private ChatMemory waveChatMemory;

    @Resource
    private VectorStore waveVectorStore;

    @Resource
    private WaveTokenCalculateAdvisor waveTokenCalculateAdvisor;

    @Resource
    private WaveAILogAdvisor waveAILogAdvisor;

    @Resource
    private WaveToolCallBackFactory waveToolCallBackFactory;

    @Bean("waveChatClient")
    @Primary
    public ChatClient waveChatClient(ChatModel dashscopeChatModel) {
        ChatClient chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem("你是智能助手阿威")
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(waveChatMemory).build(),
                        waveTokenCalculateAdvisor, // 每次会话的token计算
//                        new QuestionAnswerAdvisor(waveVectorStore), // 简单rag 知识库 回答
//                        PostDocumentRetrieverAdvisorFactory.create(waveVectorStore), // 基于 知识库的 条件 判断查询 顾问
                        waveAILogAdvisor)
//                .defaultToolCallbacks(waveToolCallBackFactory.waveToolCallbacks()) // 添加工具调用
                .build();
        return chatClient;
    }


    @Bean("doWorkChatClient")
    public ChatClient doWorkChatClient(ChatModel dashscopeChatModel) {
        ChatClient chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem("你是一个工作审批助手，要求十分严格")
                .defaultAdvisors(waveAILogAdvisor)
                .defaultToolCallbacks(waveToolCallBackFactory.waveToolCallbacks()) // 添加工具调用
                .build();
        return chatClient;
    }

}
